package com.atguigu.gmall.product.controller;
import com.atguigu.entity.*;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@RestController
@RequestMapping("/admin/product/")
public class ProductContoller {
    @Autowired
    BaseCategory1Service baseCategory1Service;
    @Autowired
    BaseCategory2Service baseCategory2Service;
    @Autowired
    BaseCategory3Service baseCategory3Service;
    @Autowired
    BaseAttrInfoService baseAttrInfoService;
    @Autowired
    BaseAttrValueService baseAttrValueService;
    @Autowired
    BaseTrademarkService trademarkService;
    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    BaseSaleAttrService baseSaleAttrService;
    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;
    @Autowired
    SkuImageService skuImageService;
    @Autowired
    SkuInfoService skuInfoService;
    /**
     * 获取一级分类
     * @return
     */
    @GetMapping("/getCategory1")
    private Result getContoller(){
        List<BaseCategory1> list = baseCategory1Service.list(null);
        return Result.ok(list);
    }
    /**
     * 获取二级分类
     * @param category1Id 一级分类ID
     * @return
     */
    @GetMapping("/getCategory2/{category1Id}")
    private Result getCategory2(@PathVariable Long category1Id){
        QueryWrapper<BaseCategory2> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("category1_id",category1Id);
        List<BaseCategory2> list = baseCategory2Service.list(objectQueryWrapper);
        return Result.ok(list);
    }
    /**
     * 获取三级分类
     * @param category2Id 2级分类Id
     * @return
     */
    @GetMapping("/getCategory3/{category2Id}")
    private Result getCategory3(@PathVariable Long category2Id){
       QueryWrapper<BaseCategory3>baseCategory3QueryWrapper = new QueryWrapper<>();
        QueryWrapper<BaseCategory3> category2_id = baseCategory3QueryWrapper.eq("category2_id", category2Id);
        List<BaseCategory3> list = baseCategory3Service.list(category2_id);
        return Result.ok(list);
    }
    /**
     * 根据传过来的分类id 获取该分类下的平台属性
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    private Result attrInfoList(@PathVariable Long category1Id,
                                @PathVariable Long category2Id,
                                @PathVariable Long category3Id){
        List<BaseAttrInfo>baseAttrInfos = baseAttrInfoService.attrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfos);
    }
    /**
     * 保存平台属性和属性值
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    private Result  saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
         baseAttrInfoService.saveAttrInfo(baseAttrInfo);
         return Result.ok();
    }
    /**
     * 根据平台属性ID获取平台属性值
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    private Result getAttrValueList(@PathVariable Long attrId){
        QueryWrapper<BaseAttrValue> baseAttrValueQueryWrapper = new QueryWrapper<>();
        baseAttrValueQueryWrapper.eq("attr_id", attrId);
        List<BaseAttrValue> list = baseAttrValueService.list(baseAttrValueQueryWrapper);
        return Result.ok(list);
    }
    /**
     * http://api.gmall.com/admin/product/baseTrademark/{page}/{limit}
     */
    /**
     * 分页查询所有的商品
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/baseTrademark/{page}/{limit}")
    private Result baseTrademark(@PathVariable Long page,@PathVariable Long limit){
        IPage<BaseTrademark> page1 = trademarkService.page(new Page<>(page, limit), null);
        return Result.ok(page1);
    }
    /**
     * 保存
     * @param baseTrademark
     * @return
     */
    @PostMapping("baseTrademark/save")
    private Result baseTrademarkSave(@RequestBody BaseTrademark baseTrademark){
        trademarkService.save(baseTrademark);
        return Result.ok();
    }
    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("baseTrademark/get/{id}")
    private Result getBaseTrademark(@PathVariable int id){
        QueryWrapper <BaseTrademark> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        BaseTrademark baseTrademark = trademarkService.getOne(queryWrapper);
        return Result.ok(baseTrademark);
    }
    /**
     * 修改商品信息
     * @param baseTrademark
     * @return
     */
    @PutMapping("/baseTrademark/update")
    private Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark){
        trademarkService.updateById(baseTrademark);
        return Result.ok();
    }

    /**
     * 删除品牌接口
     * @param id
     * @return
     */
    @DeleteMapping("/baseTrademark/remove/{id}")
    private Result removeBaseTrademark(@PathVariable Long id){
        trademarkService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据3级分类ID查询所有商品的SPU信息并且需要分页
     * @param page
     * @param limit
     * @param category3Id
     * @return
     */
   @GetMapping("/{page}/{limit}")
    public Result getSpuProduct(@PathVariable int page,
                                @PathVariable int limit,
                                @RequestParam Long category3Id)
   {
       IPage<SpuInfo> spuInfoIPage = spuInfoService.page(new Page<SpuInfo>(page, limit),
               new QueryWrapper<SpuInfo>().eq("category3_id", category3Id));
       return Result.ok(spuInfoIPage);
   }

    /**
     * 获取所有的品牌作为列表返回
     * @return
     */
   @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
       List<BaseTrademark> list = trademarkService.list(null);
       return Result.ok(list);
   }
    /**
     * 返回销售属性列表
     */
    @GetMapping("/baseSaleAttrList")
    private  Result baseSaleAttrList(){
        List<BaseSaleAttr> list = baseSaleAttrService.list(null);
        return Result.ok(list);
    }
    /**
     * 添加 SPU信息
     * @param spuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    private Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //添加 spu信息
         spuInfoService.save(spuInfo);
         //获取spu的图片信息
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        //取出图片保存
        for (SpuImage spuImage:spuImageList
             ) {
            spuImage.setSpuId(spuInfo.getId());
            spuImageService.save(spuImage);
        }
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        for (SpuSaleAttr spuSaleAttr:spuSaleAttrList
             ) {
            spuSaleAttr.setSpuId(spuInfo.getId());
            spuSaleAttrService.save(spuSaleAttr);
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            for (SpuSaleAttrValue sav:spuSaleAttrValueList
                 ) {
                sav.setSpuId(spuInfo.getId());
                spuSaleAttrValueService.save(sav);
            }
        }
        return Result.ok();
    }
    /**
     * 根据SpuID获取该商品下的所有Id信息
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    private Result spuImageList(@PathVariable Long spuId){
        List<SpuImage> spu_img_id = spuImageService.list(new QueryWrapper<SpuImage>().eq("spu_id", spuId));
        return Result.ok(spu_img_id);
    }
    /**
     * 根据SPUId查询销售属性和销售属性值集合
     * api.gmall.com/admin/product/spuSaleAttrList/1
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    private Result spuSaleAttrList(@PathVariable Long spuId){
       List<SpuSaleAttr>spuSaleAttrs = spuSaleAttrService.spuSaleAttrList(spuId);
       return Result.ok(spuSaleAttrs);
    }
    /**
     * http://api.gmall.com/admin/product/saveSkuInfo
     */
    @PostMapping("saveSkuInfo")
    private Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
             skuInfoService.saveSkuInfo(skuInfo);
            return Result.ok();
    }
    /**
     * Sku分页查询
     */
    @GetMapping("list/{page}/{limit}")
    private Result list(@PathVariable Long page, @PathVariable Long limit){
        IPage<SkuInfo> page1 = skuInfoService.page(new Page<SkuInfo>(page, limit), null);
        return Result.ok(page1);
    }
    /**
     * 下架
     */
    @GetMapping("onSale/{skuId}")
    private Result onSale(@PathVariable Long skuId){
        SkuInfo id = skuInfoService.getOne(new QueryWrapper<SkuInfo>().eq("id", skuId));
        id.setIsSale(SkuInfo.ONSALE);
        skuInfoService.update(id,new QueryWrapper<SkuInfo>().eq("id", skuId));
        return Result.ok();
    }
    /**
     * 上架
     */
    @GetMapping("/cancelSale/{skuId}")
    private Result cancelSale(@PathVariable Long skuId){
        SkuInfo id = skuInfoService.getOne(new QueryWrapper<SkuInfo>().eq("id", skuId));
        id.setIsSale(SkuInfo.CANCELSALE);
        skuInfoService.update(id,new QueryWrapper<SkuInfo>().eq("id", skuId));
        return Result.ok();
    }
}
